import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
 
	public class OldMainFrame extends JFrame
	{
		/**
		 * 
		 */
		private static final long serialVersionUID = -4754250330078925789L;
		private String[] colleges = {""};
		private String[] faculties = {""};
		private boolean selectionMade = false;
		private SelectionPanel first, second;
		private ChartPanel chart;
		private JButton go;
		protected View window = null;
		ProxyWindow proxy;
		DefaultComboBoxModel firstFacultyModel;
		DefaultComboBoxModel secondFacultyModel;
		DefaultComboBoxModel firstCollegeModel;
		DefaultComboBoxModel secondCollegeModel;
		
		JMenuItem quit;
		JMenuItem reset;
		JMenuItem console;

		public OldMainFrame(View view)
		{
			super("University Research Ranker");
			this.setSize(600, 780);
			this.window = view;
			this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	        this.addWindowListener(window);
	        
			JPanel mainPanel = new JPanel();
			mainPanel.setLayout(new FlowLayout());
			JPanel results = new JPanel();
			Border etched = BorderFactory.createEtchedBorder();
			Border titled = BorderFactory.createTitledBorder(etched, "results");
			results.setBorder(titled);
			setChart(new ChartPanel());
			chart.setSize(chart.getPreferredSize());
			results.add(getChart());
			setFirst(new SelectionPanel("First Institution", getColleges()));
			mainPanel.add(getFirst());
			second = new SelectionPanel("Second Institution", getColleges());
			mainPanel.add(second);
			mainPanel.add(createButtonPanel());
			mainPanel.add(results);
			getContentPane().add(mainPanel);
			
			JMenu menu = new JMenu("File");
	        menu.setMnemonic(KeyEvent.VK_F);
	        JMenuItem item = null;
	        //add new faculty
	        item = new JMenuItem("Add new faculty...");
	        item.setMnemonic(KeyEvent.VK_A);
	        item.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent e) 
	            {
					//JOptionPane.showMessageDialog(this,"Add Faculty");
	                System.out.println("Add Faculty");
	                window.cf.reset();
	                window.cf.setVisible(true);
	                window.cf.setAlwaysOnTop(true);
	            }
	        });
	        menu.add(item);
	        //set proxy
	        item = new JMenuItem("Set proxy...");
	        item.setMnemonic(KeyEvent.VK_P);
	        item.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent e) 
	            {
					proxy = new ProxyWindow();
	                proxy.setProxy.addActionListener(new ActionListener()
	                {
	                	public void actionPerformed(ActionEvent e) 
	                	{
	                		System.getProperties().put( "proxySet", "true" );
	                		System.getProperties().put( "proxyHost", proxy.host.getText() );
	                		System.getProperties().put( "proxyPort", proxy.port.getText() );
	                		proxy.dispose();
	                	}
	                });
	                
	                proxy.cancel.addActionListener(new ActionListener()
	                {
	                	public void actionPerformed(ActionEvent e) 
	                	{
	                		proxy.dispose();
	                	}
	                });
	                
	            }
	        });
	        menu.add(item);
	        
	        //new
	        reset = new JMenuItem("Reset");
	        reset.setMnemonic(KeyEvent.VK_N);
	        menu.add(reset);

	        //quit
	        quit = new JMenuItem("Quit");
	        quit.setMnemonic(KeyEvent.VK_Q);
	        
	        menu.add(quit);
	        
	        JMenu menu2 = new JMenu("View");
	        menu.setMnemonic(KeyEvent.VK_V);
	        JMenuItem item2 = null;
	        //Show console window
	        console = new JMenuItem("Show Console");
	        console.setMnemonic(KeyEvent.VK_C);
	        menu2.add(console);

	        JMenuBar menuBar = new JMenuBar();
	        menuBar.add(menu);
	        menuBar.add(menu2);
	        setJMenuBar(menuBar);

			setDefaultCloseOperation(EXIT_ON_CLOSE);
			setVisible(true);
		}

		/**
		 * @return
		 */
		private String[] getColleges()
		{
			return this.colleges;
		}

		public JPanel createButtonPanel()
		{
			JPanel buttonPanel = new JPanel();
			setGo(new JButton("Go!"));
			//go.addQueryListener(this);
			getGo().setActionCommand("go");
			buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
			buttonPanel.add(getGo());
			return buttonPanel;
		}
		
		public boolean checkInputs()
		{
			if(getFirst().getSelectedCollege().equals("Select College"))
			{
				JOptionPane.showMessageDialog(this,"Please choose the first college");
				return false;
			}
			if(getFirst().getSelectedFaculty().equals("Select Faculty"))
			{
				JOptionPane.showMessageDialog(this,"Please choose the first faculty");
				return false;
			}
			if(second.getSelectedCollege().equals("Select College"))
			{
				JOptionPane.showMessageDialog(this,"Please choose the second college");
				return false;
			}
			if(second.getSelectedFaculty().equals("Select Faculty"))
			{
				JOptionPane.showMessageDialog(this,"Please choose the second faculty");
				return false;
			}
			if(getFirst().getSelectedCollege().equals(second.getSelectedCollege()) && getFirst().getSelectedFaculty().equals(second.getSelectedFaculty()))
			{
				JOptionPane.showMessageDialog(this,"Please choose two different faculties to compare");
				return false;
			}
			return true;
		}
		
		public void setColleges(String[] newColleges)
		{
			this.colleges = new String[newColleges.length];
			this.colleges = newColleges;
		}

		/**
		 * 
		 */
		public void updateDropdowns() 
		{
			firstCollegeModel = new DefaultComboBoxModel (this.colleges);
			secondCollegeModel = new DefaultComboBoxModel (this.colleges);
			firstFacultyModel = new DefaultComboBoxModel(new String[]{"Select Faculty"});
			secondFacultyModel = new DefaultComboBoxModel(new String[]{"Select Faculty"});
			
			this.getFirst().collegeBox.setModel(firstCollegeModel);
			getFirst().facultyBox.setModel(firstFacultyModel);
			this.getSecond().collegeBox.setModel(secondCollegeModel);
			getSecond().facultyBox.setModel(secondFacultyModel);
		}
		
		public void updateFacultyDropdowns()
		{
		
			String [] faculties;
			String college;
			
			if(this.getFirst().collegePicked)
			{				
				college = getFirst().getSelectedCollege();
				Iterator <College> iter = window.getModel().colleges.iterator();
				while(iter.hasNext())
				{
					College c = iter.next();
					if(c.getCollegeName().equals(college))
					{
						faculties = c.returnFaculties();
						firstFacultyModel = new DefaultComboBoxModel(faculties);
						getFirst().facultyBox.setModel(firstFacultyModel);
						break;
					}
				}
			}
			
			if(this.getSecond().collegePicked)
			{
				college = getSecond().getSelectedCollege();
				Iterator <College> iter = window.getModel().colleges.iterator();
				while(iter.hasNext())
				{
					College c = iter.next();
					if(c.getCollegeName().equals(college))
					{
						faculties = c.returnFaculties();
						secondFacultyModel = new DefaultComboBoxModel(faculties);
						getSecond().facultyBox.setModel(secondFacultyModel);
						break;
					}
				}
			}
		}

		public void setChart(ChartPanel chart) 
		{
			this.chart = chart;
		}

		public ChartPanel getChart() 
		{
			return chart;
		}

		public void setGo(JButton go) 
		{
			this.go = go;
		}

		public JButton getGo() 
		{
			return go;
		}

		public void setFirst(SelectionPanel first) 
		{
			this.first = first;
		}

		public SelectionPanel getFirst() 
		{
			return first;
		}

		/**
		 * @return
		 */
		public SelectionPanel getSecond() 
		{
			return second;
		}
	}
